package com.kuangqi.paymentdemo.service.impl;

import com.google.gson.Gson;
import com.kuangqi.paymentdemo.entity.PaymentInfo;
import com.kuangqi.paymentdemo.enums.PayType;
import com.kuangqi.paymentdemo.mapper.PaymentInfoMapper;
import com.kuangqi.paymentdemo.service.PaymentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author kuanggong
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {
    /**
     * 记录支付日志信息
     *
     * @param plainText
     */
    @Override
    public void createPaymentInfo(String plainText) {
        Gson gson = new Gson();

        Map hashMap = gson.fromJson(plainText, HashMap.class);
        String out_trade_no = (String) hashMap.get("out_trade_no");
        String transaction_id = (String) hashMap.get("transaction_id");
        String trade_type = (String) hashMap.get("trade_type");
        String trade_state = (String) hashMap.get("trade_state");
        //用户实际支付金额
        Map<String, Object> amount = (Map) hashMap.get("amount");

        Integer payerTotal = ((Double) amount.get("payer_total")).intValue();
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(out_trade_no);
        paymentInfo.setPaymentType(PayType.WXPAY.getType());
        paymentInfo.setTransactionId(transaction_id);
        paymentInfo.setTradeType(trade_type);
        paymentInfo.setTradeState(trade_state);
        paymentInfo.setPayerTotal(payerTotal);
        //明文数据直接存库
        paymentInfo.setContent(plainText);
        baseMapper.insert(paymentInfo);

    }
}
